﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_VoteDAL : IDcms_VoteDAL
    {
        public Dcms_VoteModel AddModel(Dcms_VoteModel model)
        {
            string sQLString = "insert into Dcms_Vote(Vote_Title,Vote_Text,Vote_Value,Vote_AddTime,Vote_Content,Vote_State,Vote_CateId,Vote_Order,Vote_Num,Vote_ItemNum,Vote_ValidTime,Vote_ExFlag1,Vote_ExFlag2,Vote_ExFlag3,Vote_ExFlag4,Vote_ExFlag5,Vote_ExFlag6,Vote_ExFlag7,Vote_ExFlag8,Vote_ExFlag9,Vote_ExFlag10) values(@Vote_Title,@Vote_Text,@Vote_Value,@Vote_AddTime,@Vote_Content,@Vote_State,@Vote_CateId,@Vote_Order,@Vote_Num,@Vote_ItemNum,@Vote_ValidTime,@Vote_ExFlag1,@Vote_ExFlag2,@Vote_ExFlag3,@Vote_ExFlag4,@Vote_ExFlag5,@Vote_ExFlag6,@Vote_ExFlag7,@Vote_ExFlag8,@Vote_ExFlag9,@Vote_ExFlag10) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Vote_Title", SqlDbType.VarChar), new SqlParameter("@Vote_Text", SqlDbType.Text), new SqlParameter("@Vote_Value", SqlDbType.Text), new SqlParameter("@Vote_AddTime", SqlDbType.DateTime), new SqlParameter("@Vote_Content", SqlDbType.Text), new SqlParameter("@Vote_State", SqlDbType.VarChar), new SqlParameter("@Vote_CateId", SqlDbType.Int), new SqlParameter("@Vote_Order", SqlDbType.Int), new SqlParameter("@Vote_Num", SqlDbType.Int), new SqlParameter("@Vote_ItemNum", SqlDbType.Int), new SqlParameter("@Vote_ValidTime", SqlDbType.DateTime), new SqlParameter("@Vote_ExFlag1", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag2", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag3", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag4", SqlDbType.VarChar), 
                new SqlParameter("@Vote_ExFlag5", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag6", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag7", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag8", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag9", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag10", SqlDbType.VarChar)
             };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Vote_Title;
            cmdParms[2].Value = model.Vote_Text;
            cmdParms[3].Value = model.Vote_Value;
            cmdParms[4].Value = model.Vote_AddTime;
            cmdParms[5].Value = model.Vote_Content;
            cmdParms[6].Value = model.Vote_State;
            cmdParms[7].Value = model.Vote_CateId;
            cmdParms[8].Value = model.Vote_Order;
            cmdParms[9].Value = model.Vote_Num;
            cmdParms[10].Value = model.Vote_ItemNum;
            cmdParms[11].Value = model.Vote_ValidTime;
            cmdParms[12].Value = model.Vote_ExFlag1;
            cmdParms[13].Value = model.Vote_ExFlag2;
            cmdParms[14].Value = model.Vote_ExFlag3;
            cmdParms[15].Value = model.Vote_ExFlag4;
            cmdParms[0x10].Value = model.Vote_ExFlag5;
            cmdParms[0x11].Value = model.Vote_ExFlag6;
            cmdParms[0x12].Value = model.Vote_ExFlag7;
            cmdParms[0x13].Value = model.Vote_ExFlag8;
            cmdParms[20].Value = model.Vote_ExFlag9;
            cmdParms[0x15].Value = model.Vote_ExFlag10;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Vote_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_VoteModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_VoteModel> list = new List<Dcms_VoteModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_VoteModel item = new Dcms_VoteModel();
                if (ds.Tables[0].Rows[i]["Vote_Id"].ToString() != "") item.Vote_Id = int.Parse(ds.Tables[0].Rows[i]["Vote_Id"].ToString());
                item.Vote_Title = ds.Tables[0].Rows[i]["Vote_Title"].ToString();
                item.Vote_Text = ds.Tables[0].Rows[i]["Vote_Text"].ToString();
                item.Vote_Value = ds.Tables[0].Rows[i]["Vote_Value"].ToString();
                if (ds.Tables[0].Rows[i]["Vote_AddTime"].ToString() != "") item.Vote_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Vote_AddTime"].ToString());
                item.Vote_Content = ds.Tables[0].Rows[i]["Vote_Content"].ToString();
                item.Vote_State = ds.Tables[0].Rows[i]["Vote_State"].ToString();
                if (ds.Tables[0].Rows[i]["Vote_CateId"].ToString() != "") item.Vote_CateId = int.Parse(ds.Tables[0].Rows[i]["Vote_CateId"].ToString());
                if (ds.Tables[0].Rows[i]["Vote_Order"].ToString() != "") item.Vote_Order = int.Parse(ds.Tables[0].Rows[i]["Vote_Order"].ToString());
                if (ds.Tables[0].Rows[i]["Vote_Num"].ToString() != "") item.Vote_Num = int.Parse(ds.Tables[0].Rows[i]["Vote_Num"].ToString());
                if (ds.Tables[0].Rows[i]["Vote_ItemNum"].ToString() != "") item.Vote_ItemNum = int.Parse(ds.Tables[0].Rows[i]["Vote_ItemNum"].ToString());
                if (ds.Tables[0].Rows[i]["Vote_ValidTime"].ToString() != "") item.Vote_ValidTime = DateTime.Parse(ds.Tables[0].Rows[i]["Vote_ValidTime"].ToString());
                item.Vote_ExFlag1 = ds.Tables[0].Rows[i]["Vote_ExFlag1"].ToString();
                item.Vote_ExFlag2 = ds.Tables[0].Rows[i]["Vote_ExFlag2"].ToString();
                item.Vote_ExFlag3 = ds.Tables[0].Rows[i]["Vote_ExFlag3"].ToString();
                item.Vote_ExFlag4 = ds.Tables[0].Rows[i]["Vote_ExFlag4"].ToString();
                item.Vote_ExFlag5 = ds.Tables[0].Rows[i]["Vote_ExFlag5"].ToString();
                item.Vote_ExFlag6 = ds.Tables[0].Rows[i]["Vote_ExFlag6"].ToString();
                item.Vote_ExFlag7 = ds.Tables[0].Rows[i]["Vote_ExFlag7"].ToString();
                item.Vote_ExFlag8 = ds.Tables[0].Rows[i]["Vote_ExFlag8"].ToString();
                item.Vote_ExFlag9 = ds.Tables[0].Rows[i]["Vote_ExFlag9"].ToString();
                item.Vote_ExFlag10 = ds.Tables[0].Rows[i]["Vote_ExFlag10"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return SqlHelper.ExecuteSql("delete from Dcms_Vote where Vote_Id In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Vote where Vote_Id=@Vote_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Vote_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_VoteModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_VoteModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Vote where Vote_Id=@Vote_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Vote_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_VoteModel model = new Dcms_VoteModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_VoteModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Vote";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_VoteModel Update(Dcms_VoteModel model)
        {
            string sQLString = "update Dcms_Vote set Vote_Title=@Vote_Title,Vote_Text=@Vote_Text,Vote_Value=@Vote_Value,Vote_AddTime=@Vote_AddTime,Vote_Content=@Vote_Content,Vote_State=@Vote_State,Vote_CateId=@Vote_CateId,Vote_Order=@Vote_Order,Vote_Num=@Vote_Num,Vote_ItemNum=@Vote_ItemNum,Vote_ValidTime=@Vote_ValidTime,Vote_ExFlag1=@Vote_ExFlag1,Vote_ExFlag2=@Vote_ExFlag2,Vote_ExFlag3=@Vote_ExFlag3,Vote_ExFlag4=@Vote_ExFlag4,Vote_ExFlag5=@Vote_ExFlag5,Vote_ExFlag6=@Vote_ExFlag6,Vote_ExFlag7=@Vote_ExFlag7,Vote_ExFlag8=@Vote_ExFlag8,Vote_ExFlag9=@Vote_ExFlag9,Vote_ExFlag10=@Vote_ExFlag10 where Vote_Id=@Vote_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@Vote_Title", SqlDbType.VarChar), new SqlParameter("@Vote_Text", SqlDbType.Text), new SqlParameter("@Vote_Value", SqlDbType.Text), new SqlParameter("@Vote_AddTime", SqlDbType.DateTime), new SqlParameter("@Vote_Content", SqlDbType.Text), new SqlParameter("@Vote_State", SqlDbType.VarChar), new SqlParameter("@Vote_CateId", SqlDbType.Int), new SqlParameter("@Vote_Order", SqlDbType.Int), new SqlParameter("@Vote_Num", SqlDbType.Int), new SqlParameter("@Vote_ItemNum", SqlDbType.Int), new SqlParameter("@Vote_ValidTime", SqlDbType.DateTime), new SqlParameter("@Vote_ExFlag1", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag2", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag3", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag4", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag5", SqlDbType.VarChar), 
                new SqlParameter("@Vote_ExFlag6", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag7", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag8", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag9", SqlDbType.VarChar), new SqlParameter("@Vote_ExFlag10", SqlDbType.VarChar), new SqlParameter("@Vote_Id", SqlDbType.Int)
             };
            cmdParms[0].Value = model.Vote_Title;
            cmdParms[1].Value = model.Vote_Text;
            cmdParms[2].Value = model.Vote_Value;
            cmdParms[3].Value = model.Vote_AddTime;
            cmdParms[4].Value = model.Vote_Content;
            cmdParms[5].Value = model.Vote_State;
            cmdParms[6].Value = model.Vote_CateId;
            cmdParms[7].Value = model.Vote_Order;
            cmdParms[8].Value = model.Vote_Num;
            cmdParms[9].Value = model.Vote_ItemNum;
            cmdParms[10].Value = model.Vote_ValidTime;
            cmdParms[11].Value = model.Vote_ExFlag1;
            cmdParms[12].Value = model.Vote_ExFlag2;
            cmdParms[13].Value = model.Vote_ExFlag3;
            cmdParms[14].Value = model.Vote_ExFlag4;
            cmdParms[15].Value = model.Vote_ExFlag5;
            cmdParms[0x10].Value = model.Vote_ExFlag6;
            cmdParms[0x11].Value = model.Vote_ExFlag7;
            cmdParms[0x12].Value = model.Vote_ExFlag8;
            cmdParms[0x13].Value = model.Vote_ExFlag9;
            cmdParms[20].Value = model.Vote_ExFlag10;
            cmdParms[0x15].Value = model.Vote_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return SqlHelper.ExecuteSql("Update Dcms_Vote Set Vote_State='" + StateValue.ToString() + "' where Vote_Id In (" + AllID + ")", null) > 0;
        }
    }
}

